﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Collections.Generic;
using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Helper
{   /// <summary>
    /// copyright:kklldog
    /// date:2011-08-29
    ///  qq:17014842
    /// </summary>
    public class PlayListHelper
    {
        /// <summary>
        /// 是否正在初始化
        /// </summary>
        private static bool IsInit = true;
        /// <summary>
        /// 是否已经初始化完成
        /// </summary>
        public static bool IsInited = false;
        private static int _songIndex = 0;
        public static int SongIndex
        {
            get { return _songIndex; }
            set
            {
                _songIndex = value;
            }
        }
        private static ChannelType _CurrentChannel = ChannelType.None;
        public static ChannelType CurrentChannel
        {
            get { return _CurrentChannel; }
            set { _CurrentChannel = value; }
        }
        private static int InitChannelIndex
        { get; set; }
        private static List<ChannelType> ChannelList = new List<ChannelType>();
        private static WebClient wc = new WebClient();
        public static dbPlayList pl;
        public static event Action ReFreshComplete;
        public static event Action InitChannelComplete;
        static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Result != "")
            {
                pl = (dbPlayList)JsonConvert.DeserializeObject(e.Result, typeof(dbPlayList));
                DbHelper.DelSongByChannel(CurrentChannel);
                foreach (var item in pl.song)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    item.channel = (int)CurrentChannel;
                    DbHelper.AddSong(item);
                }
                if (ReFreshComplete != null && !IsInit)
                {
                    ReFreshComplete();
                    return;
                }
                if (IsInit)
                {
                    InitChannelIndex++;
                    if (InitChannelIndex < ChannelList.Count)
                    {
                        ReFresh(ChannelList[InitChannelIndex]);
                    }
                    else if(InitChannelComplete!=null)
                    {
                        IsInit = false;
                        IsInited = true;
                        _CurrentChannel = ChannelType.None;
                        pl.song = null;
                        InitChannelComplete();
                    }
                }
            }
        }


        public static void InitChannel()
        {
            if (ChannelList.Count == 0)
            {
                ChannelList.Add(ChannelType.华语);
                ChannelList.Add(ChannelType.欧美);
                ChannelList.Add(ChannelType.日语);
                ChannelList.Add(ChannelType.粤语);
                ChannelList.Add(ChannelType.韩语);
            }
            DbHelper.DelSongNotFav();
            IsInit = true;
            InitChannelIndex = 0;
            ReFresh(ChannelList[InitChannelIndex]);
        }

        public static List<dbSong> GetEachChannel1stSong()
        {
            List<dbSong> list = new List<dbSong>();
            foreach (var item in ChannelList)
            {
                var temp = DbHelper.GetListByChannel(item);
                if (temp != null && temp.Count > 0)
                {
                    list.Add(temp[0]);
                }
            }
            var fav = DbHelper.GetFavSongList();
            if (fav != null && fav.Count > 0)
            {
                list.Add(fav[0]);
            }
            return list;
        }
        public static void ReFresh(ChannelType t)
        {
            CurrentChannel = t;
            if (wc.IsBusy)
                wc.CancelAsync();
            SongIndex = 0;
            int channel = (int)t;

            if (channel == 0)//fav
            {
                GetFavList();
                return;
            }
            GetListByChannel(t);
            if (pl.song == null || pl.song.Count <= 0)
            {
                Random rdm = new Random();
                int r = rdm.Next();
                string url = "http://douban.fm/j/mine/playlist?type=n&h=&channel=" + channel + "&from=mainsite&r=" + r;

                wc.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                wc.DownloadStringAsync(new Uri(url, UriKind.Absolute));
            }
            else
            {
                if (ReFreshComplete != null)
                    ReFreshComplete();
            }
        }

        private static void GetFavList()
        {
           pl.song = DbHelper.GetFavSongList();
           if (ReFreshComplete != null)
               ReFreshComplete();
        }

        private static void GetListByChannel(ChannelType t)
        {
            if (pl == null)
                pl = new dbPlayList();
             pl.song= DbHelper.GetListByChannel(t);
        }
        


    }
}
